package com.ruoyi.web.controller.veterinary;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.veterinary.domain.VetBill;
import com.ruoyi.veterinary.service.IVetBillService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 账单管理Controller
 * 
 * @author ruoyi
 * @date 2024-01-19
 */
@RestController
@RequestMapping("/veterinary/bill")
public class VetBillController extends BaseController
{
    @Autowired
    private IVetBillService vetBillService;

    /**
     * 查询账单管理列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:bill:list')")
    @GetMapping("/list")
    public TableDataInfo list(VetBill vetBill)
    {
        startPage();
        List<VetBill> list = vetBillService.selectVetBillList(vetBill);
        return getDataTable(list);
    }

    /**
     * 导出账单管理列表
     */
    @PreAuthorize("@ss.hasPermi('veterinary:bill:export')")
    @Log(title = "账单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, VetBill vetBill)
    {
        List<VetBill> list = vetBillService.selectVetBillList(vetBill);
        ExcelUtil<VetBill> util = new ExcelUtil<VetBill>(VetBill.class);
        util.exportExcel(response, list, "账单管理数据");
    }

    /**
     * 获取账单管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('veterinary:bill:query')")
    @GetMapping(value = "/{billId}")
    public AjaxResult getInfo(@PathVariable("billId") Long billId)
    {
        return success(vetBillService.selectVetBillByBillId(billId));
    }

    /**
     * 新增账单管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:bill:add')")
    @Log(title = "账单管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody VetBill vetBill)
    {
        return toAjax(vetBillService.insertVetBill(vetBill));
    }

    /**
     * 修改账单管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:bill:edit')")
    @Log(title = "账单管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody VetBill vetBill)
    {
        return toAjax(vetBillService.updateVetBill(vetBill));
    }

    /**
     * 删除账单管理
     */
    @PreAuthorize("@ss.hasPermi('veterinary:bill:remove')")
    @Log(title = "账单管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{billIds}")
    public AjaxResult remove(@PathVariable Long[] billIds)
    {
        return toAjax(vetBillService.deleteVetBillByBillIds(billIds));
    }

    /**
     * 查询累计未支付账单列表
     */
    @GetMapping("/unpaid")
    public AjaxResult getUnpaidList()
    {
        List<VetBill> list = vetBillService.selectUnpaidBillList();
        return success(list);
    }

    /**
     * 生成账单编号
     */
    @GetMapping("/generateBillNo")
    public AjaxResult generateBillNo()
    {
        String billNo = vetBillService.generateBillNo();
        return success(billNo);
    }

    /**
     * 查询账单统计信息
     */
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        Map<String, Object> statistics = vetBillService.selectBillStatistics();
        return success(statistics);
    }

    /**
     * 生成客户账单
     */
    @PreAuthorize("@ss.hasPermi('veterinary:bill:add')")
    @Log(title = "生成客户账单", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public AjaxResult generateCustomerBill(@RequestParam("customerId") Long customerId,
                                          @RequestParam("billPeriodStart") Date billPeriodStart,
                                          @RequestParam("billPeriodEnd") Date billPeriodEnd)
    {
        try {
            return toAjax(vetBillService.generateCustomerBill(customerId, billPeriodStart, billPeriodEnd));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    /**
     * 更新账单支付状态
     */
    @PreAuthorize("@ss.hasPermi('veterinary:bill:edit')")
    @Log(title = "更新支付状态", businessType = BusinessType.UPDATE)
    @PostMapping("/payment")
    public AjaxResult updatePaymentStatus(@RequestParam("billId") Long billId,
                                         @RequestParam("paidAmount") BigDecimal paidAmount)
    {
        try {
            return toAjax(vetBillService.updatePaymentStatus(billId, paidAmount));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }
}
